import 'package:base_lib/common/base/base_theme_state.dart';
import 'package:base_lib/common/index.dart';
import 'package:base_lib/router/routers.dart';
import 'package:base_lib/theme/theme_global.dart';
import 'package:base_lib/utils/index.dart';
import 'package:base_lib/utils/log.dart';
import 'package:base_lib/utils/screen_adapter.dart';
import 'package:doctor_prescribe_lib/chinese_medicine/prescription_page/bloc.dart';
import 'package:doctor_prescribe_lib/doctor_prescribe_component.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../western_medicine/medical_search/entity/medical_search_entity.dart'
    as MedicalSearchBean;

///中药-编辑用药
class ChineseEditMedicinePage extends StatefulWidget {
  final Parameters parameters;

  ChineseEditMedicinePage(this.parameters);

  @override
  State<StatefulWidget> createState() {
    return _State();
  }
}

class _State extends BaseThemeState<ChineseEditMedicinePage> {
  @override
  void dispose() {
    super.dispose();
  }

  var tsBlack = TextStyle(color: Colors.black, fontSize: 16);
  var tsGrey = TextStyle(color: Colors.black, fontSize: 14);

  @override
  Widget buildChild(BuildContext context, MTheme theme) {
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(
          onPressed: () {
            Routers.pop(context);
          },
        ),
        centerTitle: true,
        title: Text('编辑用药'),
        actions: [
          FlatButton(
              onPressed: () {
                Routers.pop(context, true);
              },
              child: Text(
                '保存',
                style: tsBlack.copyWith(color: Colors.white),
              ))
        ],
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        color: theme.themeColor.themeBgColor,
        child: Wrap(
          children: _buildItems(theme),
        ),
      ),
    );
  }

  _buildItems(MTheme theme) {
    var widgets = <Widget>[];

    var itemWidth = (ScreenAdapter.screenWidthDp - 40) / 2;

    var itemHeight = itemWidth * 2.6 / 5;

    var add = GestureDetector(
      onTap: () {
        Routers.navigateTo(
                context, DoctorPrescribeComponent.chineseMedicineSearchPage,
                parameters: widget.parameters)
            .then((value) {
          if (value == true) {
            setState(() {});
          }
        });
      },
      child: Container(
        width: itemWidth,
        height: itemHeight,
        margin: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
            border: Border.all(color: theme.themeColor.themeColor),
            color: Colors.white,
            borderRadius: BorderRadius.circular(15)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              Icons.add,
              color: theme.themeColor.themeColor,
            ),
            Text(
              '添加药品',
              style: tsBlack.copyWith(color: theme.themeColor.themeColor),
            )
          ],
        ),
      ),
    );

    widgets.add(add);

    ChinesePrescriptionPageBloc.rxMedicineList.forEach((item) {
      var w = Container(
        margin: EdgeInsets.symmetric(vertical: 20, horizontal: 10),
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(15)),
        width: itemWidth,
        height: itemHeight,
        child: Stack(
          fit: StackFit.expand,
          children: [
            GestureDetector(
              onTap: () {
                jump2Dosage(item);
              },
              child: Container(
                color: Colors.white,
                margin: EdgeInsets.all(5),
                padding: EdgeInsets.all(5),
                width: itemWidth,
                height: itemHeight,
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Expanded(
                              child: Text(
                            item.medicineName,
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                            style: tsBlack.copyWith(
                                fontWeight: FontWeight.bold, fontSize: 16),
                          )),
                          Text(
                            item.medicineAmount.toString() ?? '',
                            style: tsBlack,
                          ),
                          3.hGapDp,
                          Text(
                            '克',
                            style: tsGrey,
                          )
                        ],
                      ),
                      Text(
                        item.usage ?? '',
                        style: tsGrey,
                      )
                    ],
                  ),
                ),
              ),
            ),
            Positioned(
                top: -15,
                right: -15,
                child: IconButton(
                    icon: Icon(
                      Icons.remove_circle_outline,
                      color: Colors.red,
                      size: 18,
                    ),
                    onPressed: () {
                      Custom_iOS_Dialog.prompt(context,
                          content: '确定删除  ${item.medicineName}  ?', okFn: () {
                        ChinesePrescriptionPageBloc.rxMedicineList.remove(item);
                        setState(() {});
                      });
                    }))
          ],
        ),
      );

      widgets.add(w);
    });

    return widgets;
  }

  void jump2Dosage(MedicalSearchBean.Datas item) {
    FLog('jump2Dosage');
    Routers.navigateTo(context, DoctorPrescribeComponent.chineseDosagePage,
            parameters: Parameters()..putObj('item', item))
        .then((value) {
      FLog(value);
      if (value == true) {
        setState(() {});
      }
    });
  }
}
